<template>
	<view>
		<view class="form">
			<view class="form-item">
				<view class="label">
					头像：
				</view>
				<button class="avatar-wrapper" open-type="chooseAvatar" @chooseavatar="changeTx">
					<u-avatar shape="circle" size="120rpx" :src="userInfo.avatar"></u-avatar>
				</button>
			</view>
			<view class="form-item">
				<view class="label">
					昵称：
				</view>
				<u-input v-model="userInfo.nickname" inputAlign="right" border="none" placeholder="请输入昵称"
					type="nickname" />
			</view>
			<view class="form-item">
				<view class="label">
					标签:
				</view>
				<view class="check">
					<view class="check-option" :class="userInfo.label==1?'active':''" @click="chageUserLabel(1)">
						i人
					</view>
					<view class="check-option" :class="userInfo.label==2?'active':''" @click="chageUserLabel(2)">
						e人
					</view>
				</view>
			</view>
			<view class="form-item" style="border-bottom: 0 none;">
				<view class="label">
					个性化标签:
				</view>
				<view class="add" @click="addBQ">
					+添加
				</view>
			</view>
			<view class="tips">
				<view class="tips-item" v-for="(item,index) in userInfo.biaoqian">
					{{item}}
					<view style="width: 8rpx;"></view>
					<u-icon name="close" color="#F4BD38" size="22rpx" @click="delBiaoqian(index)"></u-icon>
				</view>
			</view>
		</view>
		<view class="submit-tc">
		</view>
		<view class="submit flexAJC" @click="saveUser">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {}
			};
		},
		onLoad() {
			this.getUserInfo()
		},
		methods: {
			changeTx(res) {
				uni.uploadFile({
					url: this.$serverUrl + '/api/upload/uploadImg', //仅为示例，非真实的接口地址
					filePath: res.detail.avatarUrl,
					name: 'file',
					success: (uploadFileRes) => {
						this.userInfo.avatar = JSON.parse(uploadFileRes.data).data.image
					}
				});
			},
			getUserInfo() {
				this.$api.user.userIndex().then(res => {
					{
						this.userInfo = res.data.user
					}
				})
			},
			chageUserLabel(label) {
				this.userInfo.label = label
			},
			delBiaoqian(idx) {
				this.userInfo.biaoqian.splice(idx, 1)
				this.$forceUpdate()
			},
			addBQ() {
				uni.showModal({
					title: '提示',
					placeholderText: '请输入个性化标签',
					editable: true,
					success: (res) => {
						if (res.confirm) {
							if (uni.$u.test.isEmpty(res.content)) {
								uni.showToast({
									title: "个性化标签不能为空",
									icon: "none"
								})
								return false
							}
							this.userInfo.biaoqian.push(res.content)
						}
					}
				});
			},
			saveUser() {
				if (uni.$u.test.isEmpty(this.userInfo.label)) {
					uni.showToast({
						title: "标签不能为空",
						icon: "none"
					})
					return false
				}
				this.$api.user.doinfo({
					nickname: this.userInfo.nickname,
					avatar: this.userInfo.avatar,
					label: this.userInfo.label,
					biaoqian: this.userInfo.biaoqian.join(',')
				}).then(res => {
					uni.showToast({
						title: "保存成功",
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.form {
		padding: 0 28rpx;
		width: 100%;

		.form-item {
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 32rpx 0;
			border-bottom: 1px solid #E2E2E2;

			.label {
				font-weight: bold;
				color: #282828;
				flex-shrink: 0;
				white-space: nowrap;
			}

			.tx {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}

			.check {
				display: flex;
				align-items: center;

				.check-option {
					width: 98rpx;
					height: 48rpx;
					background: #ECECEC;
					border-radius: 24rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					font-size: 28rpx;
					color: #343434;
					border: 1px solid #ECECEC;
					margin-left: 45rpx;

					&.active {
						border: 1px solid #F4BD38;
						background: #FBFAF6;
						color: #F4BD38;
					}
				}
			}

			.add {
				color: #F4BD38;
			}



		}

		.tips {
			overflow: hidden;
			width: 100%;

			.tips-item {
				float: left;
				margin-right: 44rpx;
				width: auto;
				height: 48rpx;
				background: #FBFAF6;
				border-radius: 24rpx;
				border: 1px solid #F4BD38;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 22rpx;
				color: #F4BD38;
				padding: 0 20rpx;
				margin-bottom: 20rpx;
			}
		}
	}

	.submit-tc {
		height: 200rpx;
	}

	.submit {
		width: 485rpx;
		height: 112rpx;
		background: #F4BD38;
		border-radius: 55rpx;
		color: #FFFFFF;
		font-weight: bold;
		font-size: 26rpx;
		position: fixed;
		bottom: 82rpx;
		left: 50%;
		transform: translateX(-50%);
	}

	.avatar-wrapper {
		padding: 0;
		display: inline-block;
		border: 0 none;
		margin: 0;
		background-color: #fff;

		&::after {
			border: none;
		}
	}
</style>